package com.yanqu.road.dao.impl.activity.pawnshop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopItem;

public class UserPawnshopItemDaoImpl extends TempDataObjectDao<UserPawnshopItem> {

	public static String getUserPawnshopItemSql = "select * from `t_u_activity_pawnshop_item` where `userId`=? and `activityId`=?";

	public Map<Long, UserPawnshopItem> getUserPawnshopItem(long userId, int activityId) {
		Map<Long, UserPawnshopItem> map = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserPawnshopItemSql);
			int i = 1;
			call.setLong(i++, userId);
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPawnshopItem data = parse(rs);
				map.put(data.getItemId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return map;
	}

	public static String updateUserPawnshopItemSql = "update `t_u_activity_pawnshop_item` set repairTimes=?,fakeStar=?,opStatus=?," +
			"opReceptionNum=?,goodsId=?,level=?,rarity=?,status=?,coinBuy=?,affixSn1=?,affixSn2=?,affixSn3=?,affixSn4=?," +
			"basePrice=?,statusEffectPrice=?,eventAffixAddition=?,personAffixAddition=?,descNo = ? where userId=? and activityId=? and itemId=?";

	@Override
	protected boolean updateData(UserPawnshopItem userPawnshopItem) {
		userPawnshopItem.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getRepairTimes()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getFakeStar()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getOpStatus()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getOpReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getGoodsId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getLevel()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getRarity()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getStatus()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getCoinBuy()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn1()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn2()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn3()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn4()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getBasePrice()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getStatusEffectPrice()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getEventAffixAddition()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getPersonAffixAddition()));
		para.put(i++, new DbParameter(Types.NVARCHAR, userPawnshopItem.getDescNo()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getItemId()));
		if (getDb().executeNoneQuery(updateUserPawnshopItemSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserPawnshopItemSql = "insert into `t_u_activity_pawnshop_item`(userId,activityId,itemId,repairTimes," +
			"fakeStar,opStatus,opReceptionNum,goodsId,level,rarity,status,coinBuy,affixSn1,affixSn2,affixSn3,affixSn4,basePrice," +
			"statusEffectPrice,eventAffixAddition,personAffixAddition,descNo) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	@Override
	protected boolean addData(UserPawnshopItem userPawnshopItem) {
		userPawnshopItem.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getItemId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getRepairTimes()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getFakeStar()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getOpStatus()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getOpReceptionNum()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getGoodsId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getLevel()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getRarity()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getStatus()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getCoinBuy()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn1()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn2()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn3()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getAffixSn4()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopItem.getBasePrice()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getStatusEffectPrice()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getEventAffixAddition()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopItem.getPersonAffixAddition()));
		para.put(i++, new DbParameter(Types.NVARCHAR, userPawnshopItem.getDescNo()));
		if (getDb().executeNoneQuery(insertUserPawnshopItemSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserPawnshopItem parseData(ResultSet rs) throws SQLException {
		UserPawnshopItem data = new UserPawnshopItem();
		data.setUserId(rs.getLong("userId"));
		data.setActivityId(rs.getInt("activityId"));
		data.setItemId(rs.getLong("itemId"));
		data.setRepairTimes(rs.getInt("repairTimes"));
		data.setFakeStar(rs.getInt("fakeStar"));
		data.setOpStatus(rs.getInt("opStatus"));
		data.setOpReceptionNum(rs.getInt("opReceptionNum"));
		data.setGoodsId(rs.getInt("goodsId"));
		data.setLevel(rs.getInt("level"));
		data.setRarity(rs.getInt("rarity"));
		data.setStatus(rs.getInt("status"));
		data.setCoinBuy(rs.getLong("coinBuy"));
		data.setAffixSn1(rs.getInt("affixSn1"));
		data.setAffixSn2(rs.getInt("affixSn2"));
		data.setAffixSn3(rs.getInt("affixSn3"));
		data.setAffixSn4(rs.getInt("affixSn4"));
		data.setBasePrice(rs.getLong("basePrice"));
		data.setStatusEffectPrice(rs.getInt("statusEffectPrice"));
		data.setEventAffixAddition(rs.getInt("eventAffixAddition"));
		data.setPersonAffixAddition(rs.getInt("personAffixAddition"));
		data.setDescNo(rs.getString("descNo"));
		return data;
	}

}
